package net.shop.action.front;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import net.shop.biz.db.ProductService;
import net.shop.biz.service.FriendlyUrlService;
import net.shop.dal.entity.Product;
import ths.web.action.ActionContext;
import ths.web.action.ActionException;
import ths.web.action.Result;
import ths.web.action.exception.ActionNotFoundException;
import ths.web.action.result.ResultView;
import ths.web.annotation.HttpAction;
import ths.web.util.ParamUtils;

@HttpAction
public class ProductDetailAction extends BaseFrontAction {

	@Resource
	private ProductService productService;
	@Resource
	private FriendlyUrlService friendlyUrlService;
	
	@Override
	public Result execute() throws ActionException {
		ActionContext context = ActionContext.getContext();	
		HttpServletRequest request = context.getRequest();
		
		String friendlyUrl = ParamUtils.getTrimParameter(request, "friendly_url");
		if (StringUtils.isEmpty(friendlyUrl)) {
			throw new ActionNotFoundException("Proudct not found.");
		}
		
		long haseCode = friendlyUrlService.getUrlHaseCode(friendlyUrl);
		short languageId = context.getLocaleInfo().getLanguage().getLanguageId();
		
		Product product = productService.findProductByHaseCode(languageId, haseCode);
		if (product == null) {
			throw new ActionNotFoundException("Proudct not found.");
		}
		
		ResultView view = getViewResult(context);
		view.assign("username", "Jack");
		view.assign("proudct", product);
		
		return view.toView("product_detail");
	}
	
}